package com.pinyoumei.app.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.ZbkOrderDetailListAdapter;
import com.pinyoumei.app.bean.ZBKOrderListDetailBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-06-30.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-06-30        Administrator             新增Person类
 * 2021-06-30        Administrator             增加sex属性
 * 我的订单详情
 */
public class ZBkOrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private  String order_id = "";
    private TextView tv_order_status_name;
    private TextView tv_order_status_desc;
    private TextView tv_recieve_name;
    private TextView tv_recieve_phone;
    private TextView tv_recieve_address;
    private ZbkOrderDetailListAdapter zbkOrderDetailListAdapter;
    private TextView tv_total_num;
    private TextView tv_total_price;
    private TextView tv_express_price;
    private TextView tv_coupon_price;
    private TextView tv_coupon_price_amt;
    private TextView tv_order_price;
    private TextView tv_order_no;
    private TextView tv_order_time;
    private TextView tv_pay_time;
    private TextView tv_send_time;
    private TextView tv_receive_time;
    private TextView tv_bill_no_str;
    private TextView tv_order_express_name;
//    private TextView tv_order_remark;
//    private TextView tv_platform_remark;
    private String order_no = "";
    private String bill_no = "";
    private String express_no = "";
    private String kf_url = "";
    private TextView tv_kd_no;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zbk_order_detail);

        order_id = getIntent().getStringExtra("order_id");


        InitWidget();

        if (!TextUtils.isEmpty(order_id)){
            InitData();
        }

    }

    private void InitWidget() {

        tv_order_status_name = findViewById(R.id.tv_order_status_name);
        tv_order_status_desc = findViewById(R.id.tv_order_status_desc);
        tv_recieve_name = findViewById(R.id.tv_recieve_name);
        tv_recieve_phone = findViewById(R.id.tv_recieve_phone);
        tv_recieve_address = findViewById(R.id.tv_recieve_address);

        RecyclerView rv_1 = findViewById(R.id.rv_1);
        rv_1.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        zbkOrderDetailListAdapter = new ZbkOrderDetailListAdapter(R.layout.item_zbk_order_info,null);
        rv_1.setAdapter(zbkOrderDetailListAdapter);

        tv_total_num = findViewById(R.id.tv_total_num);
        tv_total_price = findViewById(R.id.tv_total_price);
        tv_express_price = findViewById(R.id.tv_express_price);
        tv_coupon_price = findViewById(R.id.tv_coupon_price);
        tv_coupon_price_amt = findViewById(R.id.tv_coupon_price_amt);
        tv_order_price = findViewById(R.id.tv_order_price);
        tv_order_no = findViewById(R.id.tv_order_no);
        tv_order_time = findViewById(R.id.tv_order_time);
        tv_pay_time = findViewById(R.id.tv_pay_time);
        tv_send_time = findViewById(R.id.tv_send_time);
        tv_receive_time = findViewById(R.id.tv_receive_time);
        tv_bill_no_str = findViewById(R.id.tv_bill_no_str);
        tv_order_express_name = findViewById(R.id.tv_order_express_name);
//        tv_order_remark = findViewById(R.id.tv_order_remark);
//        tv_platform_remark = findViewById(R.id.tv_platform_remark);

        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);

        TextView tv_copy_no = findViewById(R.id.tv_copy_no);
        tv_copy_no.setOnClickListener(this);

        TextView tv_pay_no = findViewById(R.id.tv_pay_no);
        tv_pay_no.setOnClickListener(this);

        tv_kd_no = findViewById(R.id.tv_kd_no);
        tv_kd_no.setOnClickListener(this);

        LinearLayout ll_kf = findViewById(R.id.ll_kf);
        ll_kf.setOnClickListener(this);
    }

    private void InitData() {

        Map<String,Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ZBkOrderDetailActivity.this));
        map.put("order_id", order_id);

        OkGo.<String>post( Constants.ZBK_ORDER_DETAIL)
                .params("user_id", SPUtils.getAppUserId(ZBkOrderDetailActivity.this))
                .params("order_id", order_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }



                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        ZBKOrderListDetailBean zbkOrderListDetailBean = gson.fromJson(response.body(), ZBKOrderListDetailBean.class);
                        if (zbkOrderListDetailBean.getCode() >= 0 ){

                            tv_order_status_name.setText(zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_status_name()+"");

                            tv_order_status_desc.setText(zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_status_desc()+"");

                            tv_recieve_name.setText("收货人："+ zbkOrderListDetailBean.getResult().getOrder_detail().getReceive_name()+"");
                            tv_recieve_phone.setText(zbkOrderListDetailBean.getResult().getOrder_detail().getReceive_phone()+"");
                            tv_recieve_address.setText("收货地址："+zbkOrderListDetailBean.getResult().getOrder_detail().getReceive_address()+"");

                            zbkOrderDetailListAdapter.setNewData(zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getGoods_list());

                            tv_total_num.setText("×"+ zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getTotal_num()+"");

                            tv_total_price.setText( zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getTotal_price()
                                    + zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getUnit_name());

                            tv_express_price.setText( zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getExpress_price()+"");

                            tv_express_price.setText(zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getExpress_price()+"");

                            tv_coupon_price.setText(zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getCoupon_price()
                                    + zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getUnit_name());

                            tv_coupon_price_amt.setText("-"+zbkOrderListDetailBean.getResult().getOrder_detail().getRed_package_amount());

                            tv_order_price.setText( zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getOrder_price()
                                    + zbkOrderListDetailBean.getResult().getOrder_detail().getGoods_info().getUnit_name());

                            order_no =   zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_no();

                            tv_order_no.setText("订单编号：" + order_no );

                            tv_order_time.setText("下单时间：" + zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_time());

                            tv_pay_time.setText("支付时间：" + zbkOrderListDetailBean.getResult().getOrder_detail().getPay_time());

                            tv_send_time.setText("发货时间：" + zbkOrderListDetailBean.getResult().getOrder_detail().getSend_time());

                            tv_receive_time.setText("收货时间：" + zbkOrderListDetailBean.getResult().getOrder_detail().getReceive_time());

                            bill_no = zbkOrderListDetailBean.getResult().getOrder_detail().getBill_no();

                            tv_bill_no_str.setText("支付单号：" + zbkOrderListDetailBean.getResult().getOrder_detail().getBill_no_str());


                            express_no = zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_express_no();

                            if (TextUtils.isEmpty(zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_express_no())){
                                tv_order_express_name.setText("快递单号：暂无快递信息" );
                                tv_kd_no.setVisibility(View.GONE);
                            }else{
                                tv_order_express_name.setText("快递单号：" + zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_express_str());
                                tv_kd_no.setVisibility(View.VISIBLE);
                            }


//                            if (TextUtils.isEmpty(zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_remark())){
//                                tv_order_remark.setText("暂无备注");
//
//                            }else{
//                                tv_order_remark.setText( zbkOrderListDetailBean.getResult().getOrder_detail().getOrder_remark());
//                            }
//
//                            if (TextUtils.isEmpty(zbkOrderListDetailBean.getResult().getOrder_detail().getPlatform_remark())){
//                                tv_platform_remark.setText("暂无备注");
//
//                            }else{
//                                tv_platform_remark.setText( zbkOrderListDetailBean.getResult().getOrder_detail().getPlatform_remark());
//                            }

                            kf_url = zbkOrderListDetailBean.getResult().getOrder_detail().getKf_url();

                        }else{
                            ToastUtils.showShort(zbkOrderListDetailBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });




    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        	    finish();
        		break;

        		case R.id.tv_copy_no:
                    //复制
                    if (!TextUtils.isEmpty(order_no)){
                        try {
                            //获取剪贴板管理器
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            // 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText("Label", order_no);
                            // 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);
                            ToastUtils.showShort("订单编号复制成功");
                            return ;
                        } catch (Exception e) {
                            return ;
                        }
                    }

        		break;

        		case R.id.tv_pay_no:
                    //复制
                    if (!TextUtils.isEmpty(bill_no)){
                        try {
                            //获取剪贴板管理器
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            // 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText("Label", bill_no);
                            // 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);
                            ToastUtils.showShort("支付单号复制成功");
                            return ;
                        } catch (Exception e) {
                            return ;
                        }
                    }
        		break;

        		case R.id.tv_kd_no:

                    //复制
                    if (!TextUtils.isEmpty(express_no)){
                        try {
                            //获取剪贴板管理器
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            // 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText("Label", express_no);
                            // 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);
                            ToastUtils.showShort("快递单号复制成功");
                            return ;
                        } catch (Exception e) {
                            return ;
                        }
                    }
        		break;

        		case R.id.ll_kf:

                //在线客服
                    if (kf_url!=null && !kf_url.equals("")){
                    Intent intent2 = new Intent(this, WebViewActivity.class);
                    intent2.putExtra("url",kf_url);
                    intent2.putExtra("order_title","联系客服");
                    startActivity(intent2);
                }else{
                    Toast.makeText(mContext, "暂未开通！", Toast.LENGTH_SHORT).show();
                }

        		break;

        	default:
        		break;
        }
    }


}
